﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StimulusToggleGroup : MonoBehaviour
{
    public Text participatedIdxText;

    private bool initialized = false;

    void OnEnable()
    {
        if (!initialized)
        {
            if (Serializer.records == null)
            {
                Serializer.LoadRecords();
            }

            int k = 0;

            foreach (Transform t in transform)
            {
                Text label1 = t.Find("Label").GetComponent<Text>();
                label1.text = Serializer.records[k].s_StimulusGroupIdx;


                Transform label2 = t.Find("Label (1)");
                label2.GetComponentInChildren<Text>().text = "ShowAll";

                int index = new int();
                index = k;

                label2.GetComponent<Button>().onClick.AddListener(delegate {
                    ShowParticipantList(index);
                });

                //label2.text = Serializer.records[k].particippatedList.Count == 0 ? "none" : 
                //Debug.Log(i);
                Debug.Log(k++);
            }
            initialized = true;
        }

        int i = 0;
        foreach (Transform t in transform)
        {
            if (t.GetComponent<Toggle>().isOn)
            {
                ShowParticipantList(i);
                break;
            }
            i++;
        }
    }

    private void ShowParticipantList(int i)
    {
        string str = "";
        var t = Serializer.records[i].particippatedList;
        foreach (string s in t)
        {
            str += s + " ";
        }
        participatedIdxText.text = (str == "" ? "none" : str);
        Debug.Log(i);
    }
}
